package de.hoits.android;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import de.hoits.android.database.TodoDbAdapter;

public class TodoDetails extends Activity
{
	private EditText mTitleText;
	private EditText mBodyText;
	private Long mRowId;
	private TodoDbAdapter mDbHelper;
	private Spinner mCategory;

	@Override
	protected void onCreate(final Bundle bundle)
	{
		super.onCreate(bundle);

		mDbHelper = new TodoDbAdapter(this);
		mDbHelper.open();
		setContentView(R.layout.todo_edit);
		mCategory = (Spinner) findViewById(R.id.category);
		mTitleText = (EditText) findViewById(R.id.todo_edit_summary);
		mBodyText = (EditText) findViewById(R.id.todo_edit_description);

		final Button confirmButton = (Button) findViewById(R.id.todo_edit_btn_conf);
		mRowId = null;
		final Bundle extras = getIntent().getExtras();
		mRowId = (bundle == null) ? null : (Long) bundle.getSerializable(TodoDbAdapter.KEY_ROWID);
		if (extras != null)
		{
			mRowId = extras.getLong(TodoDbAdapter.KEY_ROWID);
		}
		populateFields();
		confirmButton.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(final View view)
			{
				setResult(RESULT_OK);
				finish();
			}
		});
      		
		final Button delButton = (Button) findViewById(R.id.todo_edit_btn_del);
		delButton.setEnabled(false);
		if (mRowId != null)
		{
			// den gabs schon einmal
			delButton.setEnabled(true);
			delButton.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(final View view)
				{
					mDbHelper.deleteTodo(mRowId);
					setResult(RESULT_OK);
					finish();
				}
			});
		}
	}

	private void populateFields()
	{
		if (mRowId != null)
		{
			final Cursor todo = mDbHelper.fetchTodo(mRowId);
			startManagingCursor(todo);
			final String category = todo.getString(todo
					.getColumnIndexOrThrow(TodoDbAdapter.KEY_CATEGORY));

			for (int i = 0; i < mCategory.getCount(); i++)
			{
				final String s = (String) mCategory.getItemAtPosition(i);
				Log.e(null, s + " " + category);
				if (s.equalsIgnoreCase(category))
				{
					mCategory.setSelection(i);
				}
			}

			mTitleText
					.setText(todo.getString(todo.getColumnIndexOrThrow(TodoDbAdapter.KEY_SUMMARY)));
			mBodyText.setText(todo.getString(todo
					.getColumnIndexOrThrow(TodoDbAdapter.KEY_DESCRIPTION)));
		}
	}

	@Override
	protected void onSaveInstanceState(final Bundle outState)
	{
		super.onSaveInstanceState(outState);
		saveState();
		outState.putSerializable(TodoDbAdapter.KEY_ROWID, mRowId);
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		saveState();
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		populateFields();
	}

	private void saveState()
	{
		final String category = (String) mCategory.getSelectedItem();
		final String summary = mTitleText.getText().toString();
		final String description = mBodyText.getText().toString();

		if (mRowId == null)
		{
			if ((summary != null && summary.length() > 0))
			{
				// nur speichern wenn nicht leer ist
				final long id = mDbHelper.createTodo(category, summary, description);
				if (id > 0)
				{
					mRowId = id;
				}
			}
		}
		else
		{
			mDbHelper.updateTodo(mRowId, category, summary, description);
		}
	}
}
